package com.hunan.chinamobile.util;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class BaiduUtil {

    public static Map<String,Double> getPoint(String address){
        String ak = "t3H9pA3wUYuU9jUAcfXzdtcEmOSoOegr";
        String url = "http://api.map.baidu.com/geocoder/v2/?output=json&address="+address+"&ak="+ak;

        HttpGet httpGet = new HttpGet(url);
        HttpClient httpClient = HttpClients.createDefault();

        Map<String,Double> resMap = new HashMap<>();
        double lat = 0;
        double lng = 0;
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if(httpResponse.getStatusLine().getStatusCode() == 200){
                String strResult = EntityUtils.toString(httpResponse.getEntity());
                if(!StringUtils.isEmpty(strResult)){
                    JSONObject json = JSON.parseObject(strResult);
                    if(json != null && json.getIntValue("status") == 0){
                        JSONObject pointJson = json.getJSONObject("result");
                        lat = pointJson.getJSONObject("location").getDoubleValue("lat");
                        lng = pointJson.getJSONObject("location").getDoubleValue("lng");
                    }
                }
            }
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        }
        resMap.put("lat",lat);
        resMap.put("lng",lng);

        return resMap;
    }
}
